<?php
/**
 * JE Career package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2012 - 2013 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
jimport('joomla.filesystem.file');

class hostingrajacareerModelCareer extends JModel
{
	/**
	 * Constructor that retrieves the ID from the request
	 *
	 * @access	public
	 * @return	void
	 */
	function __construct()
	{
		parent::__construct();

		$array 						= JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	function setId($id)
	{
		// Set id and wipe data
		$this->_id					= $id;
		$this->_data				= null;
	}

	function &getData()
	{
		$row 						=& $this->getTable();
		$row->load( $this->_id );

		return $row;
	}

	function store()
	{
		global $mainframe;
		$settings 					= $this->getGlobalsettings();

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$row						=& $this->getTable('career_site');
		$cert_file					= JRequest::getVar( 'resume', '', 'files', 'array' );
		$full_name					= JRequest::getVar('fullname');
		$email						= JRequest::getVar('email');
		
		$date			  		 	= date("Y_m_d_H_i_s");
		$upload_folder 				= JText::_( 'UPLOADED_CV' );
		
		$row->resume				= hostingrajacareerModelCareer::uploadFile($cert_file,$settings);
		
		$cert_file['tmp_name']=$row->resume;
		
			$upload_directory			 = JPATH_ADMINISTRATOR.DS.'upload_hostingrajacareer';
		
		
		$upload_directory_copy			 = JPATH_BASE.DS.'images'.DS.'stories';
		
		$file_name_position = strripos($row->resume, DS);
		
		$file_name = substr ($row->resume , $file_name_position+1 );
		
		
		
		$format				  	 = strtolower(JFile::getExt($cert_file['name']));
		
		$copy_path=$upload_directory_copy.DS.$upload_folder;
		
		if(!JFolder::exists($copy_path)){
			JFolder::create($copy_path, 0775);
		}
		
		$copy_path=$copy_path.DS.$full_name."_".$date.".".$format;
		
		JFile::copy($row->resume,$copy_path);
		
		$date_time	= date("Y-m-d H:i:s");
		
		$data =new stdClass();
		$data->id = null;
		$data->date_time = $date_time;
		
		
		$data->nome = $full_name;
		$data->email = $email;
		$data->cv = $file_name;
		
		
		$db = JFactory::getDBO();
		$db->insertObject( 'test_db_candidato', $data, $data->id );
		
		
		$row->resume = $file_name;
		
		
		
		
		
//		echo $row->resume;	
		
		
		
//		exit;
		
		
		
		if($file_name =='format')
		{
			return 'format';
		}
		else if($file_name =='size')
		{
			return 'size';
		}
		else if($file_name=='notupload')
		{
			return 'notupload';
		}
		else
		{
			// Bind the form fields
			$post					= JRequest::get( 'post' );

			if (!$row->bind($post)) {
				return 0;
			}
			// Make sure data is valid
			if (!$row->check()){
				return 0;
			}
			// Store it
			if (!$row->store()){
				return 0;
			}

			if($settings->user_email){
			$this->sendMailUser($row->id,$settings);
			}
			if($settings->admin_email){
			$this->sendMail($row->id,$settings);
			}

			return $row->id;
		}
	}

	function uploadFile($cert_file,$settings)
	{
		$format_db					 =$settings->file_format;

		if($settings->file_format==''){
			$format_db				 ='doc,pdf,txt,pptx';
		}
		$a_file_format				 =explode(",",$format_db);

		$upload_directory			 = JPATH_ADMINISTRATOR.DS.'upload_hostingrajacareer';
		
		
		$upload_directory_copy			 = JPATH_BASE.DS.'images'.DS.'stories';
		
		
		
		if (isset($cert_file['name'])  && $cert_file['name'] != '') {

			$format				  	 = strtolower(JFile::getExt($cert_file['name']));
    		$date			  		 =& JFactory::getDate();
    		$file_size				 = $cert_file['size'];
			$file_name				 = time().".".$format;
			$cert_filepath_new	     = $upload_directory . DS . $file_name;
			
						
//			$cert_filepath_copy_new	     = $upload_directory_copy . DS . $file_name;
			
			
			// Check whether the file in an image format..

			$check_file="no";
			for($i = 0; $i < count($a_file_format) ; $i++){
					if($a_file_format[$i] == $format){
						$check_file	 = "yes";
						break;
					}
				}

			if($check_file=='no') {
				$status				 = 'format';
				return $status;
			} else {
				if($file_size>2097152){
					$status			 = 'size';
					return $status;
				}
				else{
//					$temp_file=$cert_file['tmp_name'];
					if (!JFile::upload($cert_file['tmp_name'], $cert_filepath_new)) {
					$status			 = 'notupload';
					return $status;
					} else {
						
						
												
//						JFile::upload($temp_file, $cert_filepath_copy_new);

						
						
						$file 		 = $cert_filepath_new;
		           		$save 		 = $cert_filepath_new;
						$photo 		 = $file_name;

						return $cert_filepath_new;
					}
				}
			}
		}
	}

	
	
	
	function uploadFile2($cert_file,$settings)
	{
		
		ini_set('display_errors',1);
error_reporting(E_ALL);
		$format_db					 =$settings->file_format;

		if($settings->file_format==''){
			$format_db				 ='doc,pdf,txt,pptx';
		}
		$a_file_format				 =explode(",",$format_db);

		$upload_directory			 = JPATH_BASE.DS.'images'.DS.'stories';
			
		
		
		if (isset($cert_file['name'])  && $cert_file['name'] != '') {

			$format				  	 = strtolower(JFile::getExt($cert_file['name']));
    		$date			  		 =& JFactory::getDate();
    		$file_size				 = $cert_file['size'];
			$file_name				 = time().".".$format;
			$cert_filepath_new	     = $upload_directory . DS . $file_name;
						
			
			// Check whether the file in an image format..

			$check_file="no";
			for($i = 0; $i < count($a_file_format) ; $i++){
					if($a_file_format[$i] == $format){
						$check_file	 = "yes";
						break;
					}
				}

			if($check_file=='no') {
				$status				 = 'format';
				return $status;
			} else {
				if($file_size>2097152){
					$status			 = 'size';
					return $status;
				}
				else{
					
					if (!JFile::upload($cert_file['tmp_name'], $cert_filepath_new)) {
					$status			 = 'notupload';
					return $status;
					} else {
										
						
						$file 		 = $cert_filepath_new;
		           		$save 		 = $cert_filepath_new;
						$photo 		 = $file_name;

						return $photo;
					}
				}
			}
		}
	}
	
	function sendMail($id,$settings){

		$row						=& $this->getTable('applied_openings');
		$row->load( $id );

		global $mainframe;
		$mainframe					=& JFactory::getApplication();

		$from 						= $row->email;
		$fromname 					= $row->fullname;
		$mailid 					= $settings->emailid;
		$subject 					= JText::_('JE_SUBJECT_USER_MAIL');
		if($row->resume){
			$attachment				= JRoute::_( 'administrator/upload_hostingrajacareer/'.$row->resume);
			$body_resume 			='';
		}
		else{
			$attachment=null;
			$body_resume 			= JText::_('JE_EMAIL_PASTED_RESUME').'<br>'.$row->text_resume ;
		}

			$body 					= JText::_('JE_EMAIL_ADMIN_BODY_START').'<br /><br />
										<table width="80%" cellpadding="0" cellspacing="0" border="1" bordercolor="#333333">
											<tr><td width="30%"><b>'.JText::_('JE_EMAIL_NAME').'</b></td><td>&nbsp;' . $row->fullname . '</td></tr>
											<tr><td><b>'.JText::_('JE_EMAIL').'</b></td><td>&nbsp;' . $row->email . '</td></tr>
											<tr><td><b>'.JText::_('JE_MOBILE').'</b></td><td>&nbsp;' . $row->mobile . '</td></tr>
											<tr><td><b>'.JText::_('JE_EMAIL_POSITION').'</b></td><td>&nbsp;' . $row->position . '</td></tr>
										</table><br><br>'.$body_resume.'
										<br><br>'.JText::_('JE_EMAIL_THANKS').'<br/>'.$row->fullname;

			$check					= JUtility::sendMail($from, $fromname, $mailid, $subject, $body , true, $cc=null,$bcc=null, $attachment);

	}

	function sendMailUser($id,$settings)
	{
		$row 						=& $this->getTable('applied_openings');
		$row->load( $id );

		global $mainframe;
		$mainframe					=& JFactory::getApplication();
		$from 						= $row->email;
		$fromname 					= $row->fullname;
		$mailid 					= $row->email;
		$subject 					= JText::_('JE_SUBJECT_USER_MAIL').' '.$row->position;
		$attachment 				= null;
		$body_resume			 	='';
		$body 						= JText::_('JE_EMAIL_USER_BODY_START').$row->fullname.',<br />
										<p>'.JText::_('JE_MAIL_TO_USER_CONTENT').'</p><br />'.JText::_('JE_MAIL_TO_USER_THANKS_START').'<br />'.JText::_('JE_MAIL_TO_USER_THANKS').'';
		$check						= JUtility::sendMail($from, $fromname, $mailid, $subject, $body , true, $cc=null,$bcc=null, $attachment);

	}

	function getGlobalsettings()
	{
		$db 						=& JFactory::getDBO();
		$query 						= 'SELECT * FROM #__hostingraja_career_settings WHERE id= 1';
		$db->setQuery( $query );

		if (!$db->query())	{
			echo $db->getErrorMsg();
		}
		$rows  						= $db->loadObject();

		return $rows;
	}

	function getAllCareers()
	{
		$all_id		   				= '';
		$db 		   				= & JFactory::getDBO();
		$rows->id[]	   				= 0;
		$query 	   					= "SELECT id FROM #__hostingraja_career_category where state = '1' ";
		$db->setQuery( $query );

		if (!$db->query())	{
			echo $db->getErrorMsg();
		}

		$rows      					= $db->loadObjectList();
		$id[] 						= 0;
		for( $i=0; $i<count($rows); $i++ ) {
			$row 					= &$rows[$i];
			$id[] 					= $row->id;
		}

		for( $i=0; $i<count($id); $i++ ) {

			if ( $i == count($id) - 1 ) {
				$val 				= '';
			} else {
				$val 				= ',';
			}
			$all_id .= $id[$i].$val;

		}

		return $all_id;
	}

}
?>